package com.weareint.xxljob.dynamic;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.DependsOn;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @see IDynamicXxlJobRegisterTask
 *     <pre>
 *  初始化全局的 动态注册任务
 * </pre>
 *
 * @author johnny
 * @date 2021-08-19 10:20 上午
 */
@Slf4j
@DependsOn("initJobGroup")
public class GlobalDynamicXxlJobRegistrar implements InitializingBean {

    private final XxlJobDynamicRegister xxlJobDynamicRegister;

    private final List<IDynamicXxlJobRegisterTask> registerServices;

    public GlobalDynamicXxlJobRegistrar(
            XxlJobDynamicRegister xxlJobDynamicRegister,
            List<IDynamicXxlJobRegisterTask> registerServices) {
        this.xxlJobDynamicRegister = xxlJobDynamicRegister;
        this.registerServices = registerServices;
        log.info(
                "【GlobalDynamicXxlJobRegistrar : {} 】",
                registerServices.stream()
                        .map(register -> register.getClass().getName())
                        .collect(Collectors.joining(",")));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        registerServices.forEach(
                registerService -> registerService.register(xxlJobDynamicRegister));
    }
}
